//
//  PTGuaranteeViewController.swift
//  PropertyClient
//
//  Created by benwang on 2021/6/22.
//

import UIKit
import CollectionKit

class GuaranteeViewController: UIViewController {

  var collectionView = CollectionView()
  let locationButtonItem = LocationButtonItem(maxLength: 12)
  let sheetHandler = PTSheetHandle()

  override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    navigationItem.leftBarButtonItem = locationButtonItem
    locationButtonItem.text = "正在定位中..."

    opacityNavigationBar(color: UIColor(hex: 0xFAFAFA))

    view.backgroundColor = UIColor(hex: 0xFAFAFA)
    view.addSubview(collectionView)

    collectionView.snp.makeConstraints { maker in
      maker.edges.equalToSuperview()
    }

    let dataSource = ArrayDataSource<GuaranteeItem>(
      data: GuaranteeItem.items
    )

    let viewSource = ClosureViewSource<GuaranteeItem, UnitImageTextView> { item, data, index in
      item.image = UIImage(named: data.icon)
      item.text = data.rawValue
    }

    let sizeSource = { (index: Int, data: GuaranteeItem, size: CGSize) in
      return CGSize(width: size.width - 30, height: 80)
    }

    let inset = UIEdgeInsets(top: 10, left: 0, bottom: 0, right: 0)

    let itemProvider = BasicProvider<GuaranteeItem, UnitImageTextView>(
      dataSource: dataSource,
      viewSource: viewSource,
      sizeSource: sizeSource,
      layout: FlowLayout(spacing: 12, justifyContent: .center)
        .inset(by: inset)
    )

    itemProvider.animator = FadeAnimator()
    itemProvider.tapHandler = { (context) in
      self.jump(to: context.data)
    }

    collectionView.provider = ComposedProvider(sections: [itemProvider])

    DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
      self.locationButtonItem.text = "老北京四合院"
    }
  }

  func jump(to item: GuaranteeItem) {
    switch item {
    case .smwx:
      self.performSegue(withIdentifier: "repair_order", sender: nil)
    case .bxdd:
      self.performSegue(withIdentifier: "report_order", sender: nil)
    case .grbx, .ggbx:
      sheetHandler.sheet(repairInfo: nil) { index in
        guard index == .ensure else { return}
        self.performSegue(withIdentifier: "transfer_order", sender: nil)
      }
    case .bxsh:
      let vc = RepairAuditViewController()
      vc.hidesBottomBarWhenPushed = true
      navigationController?.pushViewController(vc, animated: true)
    case .pjtj:
      let vc = EvaluationStatisticalViewController()
      vc.hidesBottomBarWhenPushed = true
      navigationController?.pushViewController(vc, animated: true)
    }
  }


  /*
   // MARK: - Navigation

   // In a storyboard-based application, you will often want to do a little preparation before navigation
   override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
   // Get the new view controller using segue.destination.
   // Pass the selected object to the new view controller.
   }
   */

}

enum GuaranteeItem: String {
  case smwx = "上门维修"
  case grbx = "个人报修"
  case ggbx = "公共报修"
  case bxdd = "报修订单"
  case bxsh = "报修审核"
  case pjtj = "评价统计"

  var icon: String {
    switch self {
    case .smwx:
      return "icon_smwx"
    case .grbx:
      return "icon_grbx"
    case .ggbx:
      return "icon_ggbx"
    case .bxdd:
      return "icon_bxdd"
    case .bxsh:
      return "m_bxsh"
    case .pjtj:
      return "icon_pjtj"
    }
  }

  static let items: [GuaranteeItem] = [
    .smwx, .grbx, .ggbx, .bxdd, .bxsh, .pjtj
  ]
}
